#include "actionmodel.h"

const int ActionModel::MAX_COLUMN_NUM = 2;

ActionModel::ActionModel(AnimationDataHandler* coreData, QObject* parent)
    : QAbstractListModel(parent)
{
    m_coreData = coreData;
}

ActionModel::~ActionModel()
{
}

int ActionModel::getOverZeroValue(int value)
{
    if(value < 0)
    {
        return 0;
    }
    return value;
}

bool ActionModel::isValidIndex(const QModelIndex &index) const
{

    if(!index.isValid() || index.row() < 0 ||index.row() >= m_coreData->getActionCount())
    {
        return false;
    }



    return true;
}

bool ActionModel::setDataByColumnIndex(int section, Action *action,const QVariant& value)
{
    if(action == NULL)
    {
        return false;
    }

    switch(section)
    {
        case 0:
          action->setName(value.toString());
        break;
        case 1:
          action->setDelay(value.toFloat());
        break;
        default:
            return false;
        break;
    }
    return true;

}

QVariant ActionModel::getColumnData(int section, Action* action) const
{
    if(action == NULL)
    {
        return QVariant();
    }

    switch(section)
    {
        case 0:
            return action->getName();
        break;

        case 1:
            return action->getDelay();
        break;

        default:
            return QVariant();
           break;
    }
}

QVariant ActionModel::data(const QModelIndex &index, int role) const
{
    if(!isValidIndex(index))
    {
        return QVariant();
    }

    if(role == Qt::DisplayRole || role == Qt::EditRole)
    {
        Action* action = m_coreData->getActionByIndex(index.row());
        if(action == NULL)
        {
            return QVariant();
        }

         return getColumnData(index.column(),action);
    }
    else
    {
        return QVariant();
    }
}

Qt::ItemFlags ActionModel::flags(const QModelIndex &index) const
{
    if(!isValidIndex(index))
    {
        return Qt::ItemIsEnabled;
    }

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

QVariant ActionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
            case 0:
                return tr("Name");
            case 1:
                return tr("delay");
            default:
                return QVariant();
        }
    }
    return QVariant();
}

int ActionModel::columnCount(const QModelIndex &parent) const
{
    return MAX_COLUMN_NUM;
}

int ActionModel::rowCount(const QModelIndex &parent) const
{
      return m_coreData->getActionCount();

}

bool ActionModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(!isValidIndex(index) && role != Qt::EditRole)
    {
        return false;
    }

    Action* action = m_coreData->getActionByIndex(index.row());

    bool result = setDataByColumnIndex(index.column(), action, value);

    emit dataChanged(index,index);

    return result;
}

void ActionModel::dataChangeUpdate()
{
    reset();
}

void ActionModel::selectChanged(const QItemSelection &selected, const QItemSelection &desSelected)
{
    QModelIndexList items = selected.indexes();

    if(items.count() > 0)
    {
        QModelIndex index = items.at(0);

        m_coreData->setCurrentActionByIndex(index.row());

    }
}

void ActionModel::appendNewAction()
{
    int size = m_coreData->getActionCount();
    beginInsertRows(QModelIndex(),size,size);  
        m_coreData->appendNewActionData();
    endInsertRows();
}

void ActionModel::deleteAction(QModelIndex& index)
{
    if(!isValidIndex(index))
    {
        emit notifyDataUpdatError();
        return;
    }

    beginRemoveRows(QModelIndex(), index.row(),index.row());
        m_coreData->removeActionByIndex(index.row());
    endRemoveRows();
}

QModelIndex ActionModel::getCurrentActionIndex()
{
    int row = m_coreData->getCurrentActionIndex();
    if(row != -1)
    {
        return this->index(row,0);
    }
    else
    {
        return QModelIndex();
    }
}


